<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class anggaran2 extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('mdl_anggaran', 'mdl_anggaran');		$this->load->helper('form') ;		$this->load->library('Lib_date'); 
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();				$this->open();				# config pagination		$config['base_url'] = base_url().'index.php/anggaran/index/';		$config['total_rows'] = $this->db->count_all('anggaran_post');		$config['per_page'] = '10';		$config['num_links'] = '3';		$config['uri_segment'] = '3';				$config['full_tag_open'] = '';		$config['full_tag_close'] = '';				$config['num_tag_open'] = '<div id="page-info">';		$config['num_tag_close'] = '</div>';				$config['cur_tag_open'] = '<li class="page-current"><strong>';		$config['cur_tag_close'] = '</strong></li>';				$config['prev_link'] = '<li class="page-left"></li>';		$config['prev_tag_open'] = '';		$config['prev_tag_close'] = '';				$config['next_link'] = '<li class="page-right"></li>';		$config['next_tag_open'] = '';		$config['next_tag_close'] = '';				$config['last_link'] = '<li class="page-far-right"></li>';		$config['last_tag_open'] = '';		$config['last_tag_close'] = '';				$config['first_link'] = '<li class="page-far-left"></li>';		$config['first_tag_open'] = '';		$config['first_tag_close'] = '';				$this->pagination->initialize($config);			# end config pagination				# get data				$data['results'] = $this->mdl_anggaran->getItem($config['per_page'], $this->uri->segment(3));
		# load view		$this->load->view('anggaran/anggaran_list', $data);		$this->close();
	}
	 
	function insert()
	{		 		
		if ($this->can_insert() == FALSE){
			redirect('auth/failed');
		}
		$this->open();				$config['upload_path'] =  $_SERVER['DOCUMENT_ROOT'].'/simkeu-bp3ti/upload/anggaran/';		$config['allowed_types'] = '*';		$config['max_size']  = '1000'; 		$this->load->library('upload', $config);		if ( ! $this->upload->do_upload())		{			$data['error'] 		= $this->upload->display_errors();			$data['excel'] 		= null;			$data['id_anggaran']  	= null;			$data['tanggal'] 	= null;			$data['uraian'] 	= null;			$data['tahun'] 		= null;			$data['namafile'] 	= null;			$data['tgl1']		= null;              $data['tgl2']		= null;  			$data['hide'] =0;			$this->load->view('anggaran/anggaran_add', $data); 		}		else		{												$upload_data = $this->upload->data(); 			$nama_lama =  $upload_data['file_name'];			$nama_baru =  'anggaran-'. date("Y.m.d") . '-' . date("H.i.s") .'-'.  $upload_data['raw_name'] .  $upload_data['file_ext'] ;            rename($config['upload_path'] . $nama_lama, $config['upload_path'] . $nama_baru);							$this->load->library('excel_reader'); 			$this->excel_reader->setOutputEncoding('CP1251'); 			$file = $config['upload_path'].$nama_baru; 			$this->excel_reader->read($file);			error_reporting(E_ALL ^ E_NOTICE); 			$excel = $this->excel_reader->sheets[0] ; 						$data['id_anggaran'] 	= 'anggaran-'. date("Y.m.d") . '-' . date("H.i.s");			$data['tanggal'] 	= date("Y-m-d");			$data['uraian'] 	= $this->input->post('uraian');			$data['tahun'] 	= $this->input->post('tahun');			$data['namafile'] 	= $nama_baru;						$results= $this->mdl_anggaran->cek_tahun($data['tahun']); 						foreach($results as $row) 			{				//echo $row->id_anggaran;				$this->mdl_anggaran->delete_anggaran_post($data['tahun']); 				$this->mdl_anggaran->delete_anggaran($row->id_anggaran); 			}						$this->mdl_anggaran->insert($data); 						$array_hari = array(1=>"Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu", "Minggu");			$hari = $array_hari[date("N")];            $this->load->library('Lib_date'); 			$data2['tgl1']= $this->lib_date->mysql_to_human($data['tanggal'] );              $data2['tgl2']= $this->lib_date->get_day($data['tanggal'] );  			$data2['hide'] =1;			 			//echo "<table border='3' wodth='100%'> "; 			$nilai_array_1=0;$nilai_array_2=0;$nilai_array_3=0;$nilai_array_4=0;$nilai_array_5=0;$nilai_array_6=0;$nilai_array_7=0;$nilai_array_8=0;			for ($i = 2; $i <= $excel['numRows']; $i++) {  			$hasil1=strlen(str_replace(" ", "", $excel['cells'][$i]['1']));						 								 				if(((preg_match("/[.]/",$excel['cells'][$i]['1'] ))&&($hasil1 == 8))&&(preg_match("/[0-9]/",$excel['cells'][$i]['1'] )))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>angka dan titik dan delapan</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_3=$nilai_array_3+1;					$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,'0','0','0','0','0');					//echo "<td>"; 					$pengulangan_3=0;					while ($pengulangan_3 < count($parent))					{						//echo $parent[$pengulangan_3];echo"-";						$pengulangan_3=$pengulangan_3+1;					}					 					//echo "</td>";						//echo "</tr>";					$nilai_array_4=0;				}				elseif((preg_match("/[.]/",$excel['cells'][$i]['1'] ))&&(preg_match("/[0-9]/",$excel['cells'][$i]['1'] )))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>titik dan angka</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_1=$nilai_array_1+1;					$parent = array($nilai_array_1,'0','0','0','0','0','0','0');					//echo "<td>"; 					$pengulangan_1=0;					while ($pengulangan_1 < count($parent))					{						//echo $parent[$pengulangan_1];echo"-";						$pengulangan_1=$pengulangan_1+1;					}															//echo "</td>";						//echo "</tr>";					$nilai_array_2=0; 				}				elseif(($hasil1 == 4)&&(preg_match("/[0-9]/",$excel['cells'][$i]['1'] )))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>angka dan empat</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_2=$nilai_array_2+1;					$parent = array($nilai_array_1,$nilai_array_2,'0','0','0','0','0','0');					//echo "<td>"; 					$pengulangan_2=0;					while ($pengulangan_2 < count($parent))					{						//echo $parent[$pengulangan_2];echo"-";						$pengulangan_2=$pengulangan_2+1;					}					 					//echo "</td>";						//echo "</tr>";					$nilai_array_3=0;				}								elseif(($hasil1 == 3)&&(preg_match("/[0-9]/",$excel['cells'][$i]['1'] )))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>angka dan tiga</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_4=$nilai_array_4+1;					$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,'0','0','0','0');					//echo "<td>"; 					$pengulangan_4=0;					while ($pengulangan_4 < count($parent))					{						//echo $parent[$pengulangan_4];echo"-";						$pengulangan_4=$pengulangan_4+1;					}					 					//echo "</td>";						//echo "</tr>";					$nilai_array_5=0;				}				elseif(preg_match("/[a-zA-Z]/",$excel['cells'][$i]['1'] ))				{ 					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>huruf</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_5=$nilai_array_5+1;					$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,$nilai_array_5,'0','0','0');					//echo "<td>"; 					$pengulangan_5=0;					while ($pengulangan_5 < count($parent))					{						//echo $parent[$pengulangan_5];echo"-";						$pengulangan_5=$pengulangan_5+1;					}					 					//echo "</td>";					//echo "</tr>";					$nilai_array_6=0;				}				else if(preg_match("/[0-9]/",$excel['cells'][$i]['1'] ))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>angka</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_6=$nilai_array_6+1;					$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,$nilai_array_5,$nilai_array_6,'0','0');					//echo "<td>"; 					$pengulangan_6=0;					while ($pengulangan_6 < count($parent))					{						//echo $parent[$pengulangan_6];echo"-";						$pengulangan_6=$pengulangan_6+1;					}										//echo "</td>";					//echo "</tr>";					$nilai_array_7=0;					 				}								elseif((preg_match("/[>]/",$excel['cells'][$i]['2'] )) or (preg_match("/[>>]/",$excel['cells'][$i]['2'] )))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;&nbsp;&nbsp;>&nbsp;&nbsp;&nbsp;</td>";					echo "<td>&nbsp;</td>";					*/					$nilai_array_7=$nilai_array_7+1;					$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,$nilai_array_5,$nilai_array_6,$nilai_array_7,'0');					//echo "<td>"; 					$pengulangan_7=0;					while ($pengulangan_7 < count($parent))					{						//echo $parent[$pengulangan_7];echo"-";						$pengulangan_7=$pengulangan_7+1;					}					 					//echo "</td>";					//echo "</tr>";					$nilai_array_8=0;				}				elseif(preg_match("/[-]/",$excel['cells'][$i]['2'] ))				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;</td>";					*/					if ($parent['7']==0)					{						$nilai_array_8=0;						$nilai_array_8=$nilai_array_8+1;						$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,$nilai_array_5,$nilai_array_6,$nilai_array_7,$nilai_array_8);						//echo "<td>"; 						$pengulangan_8=0;						while ($pengulangan_8 < count($parent))						{							//echo $parent[$pengulangan_8];echo"-";							$pengulangan_8=$pengulangan_8+1;						}						 						//echo "</td>";						//echo "</tr>";						}					else					{						$nilai_array_8=$nilai_array_8+1;						$parent = array($nilai_array_1,$nilai_array_2,$nilai_array_3,$nilai_array_4,$nilai_array_5,$nilai_array_6,$nilai_array_7,$nilai_array_8);						//echo "<td>"; 						$pengulangan_8=0;						while ($pengulangan_8 < count($parent))						{							//echo $parent[$pengulangan_8];echo"-";							$pengulangan_8=$pengulangan_8+1;						}						 						//echo "</td>";						//echo "</tr>";					}				}				else				{					/*					echo "<tr>";					echo "<td>".$excel['cells'][$i]['1']."</td>";					echo "<td>kosong</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "<td>&nbsp;</td>";					echo "</tr>";					*/				}											 				$data3['id_anggaran']	= $data['id_anggaran'];				$data3['kode']			= str_replace(" ", "", $excel['cells'][$i]['1']);				$data3['parent']		= $parent[0].'.'.$parent[1].'.'.$parent[2].'.'.$parent[3].'.'.$parent[4].'.'.$parent[5].'.'.$parent[6].'.'.$parent[7];				$data3['uraian']		= $excel['cells'][$i]['2'];				//$data3['uraian'] 		= str_replace(" ", "", $excel['cells'][$i]['2']);				$data3['vol']			= $excel['cells'][$i]['3'];				$data3['sat']			= $excel['cells'][$i]['4'];				$data3['hargasat']		= $excel['cells'][$i]['5'];				$data3['jumlah']		= $excel['cells'][$i]['6'];				$data3['tanggal']		= date("Y-m-d");								$this->mdl_anggaran->insert_data($data3); 								 			}										redirect('anggaran');		}		
		$this->close();
	}	 	 		function cek_tahun(){          $tahun = $_POST['tahun'];         $hasil_username = $this->mdl_anggaran->cek_tahun($tahun);        if(count($hasil_username) > 0)		{             echo '1';        }else		{            echo '2';        }    }	function verifikasi()	{		$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();		 		$this->open();				$tahun = $this->uri->segment(3);		if (!$tahun)		{			$tahun = date("Y");					}		$data['tahun'] = $tahun;		$data['tahun_tombol'] = $this->mdl_anggaran->getTahun();		$id_anggaran = $this->mdl_anggaran->cek_tahun($tahun); 						foreach($id_anggaran as $row) 			{ 				//$this->mdl_anggaran->delete_anggaran_post($data['tahun']); 				//$this->mdl_anggaran->delete_anggaran($row->id_anggaran); 				$datenow=date('Y'); 				$data['results'] 	= $this->mdl_anggaran->getItemBy($row->id_anggaran);				$data['count_result']	= sizeof($this->db->select('id') 				->from('anggaran') 				->get()				->result());   				$this->load->view('anggaran/anggaran_verifikasi', $data);			}									 		$this->close();	}		function report()	{		$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();		$this->open();		$data['tahun'] = $this->mdl_anggaran->getTahun();		$this->load->view('anggaran/anggaran_report_form', $data);		$this->close();	} 
	function report_form()	{		//$this->open();		//$periode_awal = $this->uri->segment('3');		//$periode_akhir = $this->uri->segment('4'); 		//if (($periode_awal)&&($periode_akhir)){				//	$data['periode_awal']=$periode_awal;		//	$data['periode_akhir']=$periode_akhir;		$id =  $this->uri->segment(3);		$data['results'] 	= $this->mdl_anggaran->getItemBy($id);						$this->load->view('anggaran/anggaran_report', $data);		 			//$this->close();		//}	}		function report_excel(){		header("Content-type: application/x-msdownload");		header("Content-Disposition: attachment; filename=anggaran.xls");		echo  $this->input->post('rekap_tabel1');	}	function report_pdf(){		$periode_awal = $this->uri->segment('3');		$periode_akhir = $this->uri->segment('4');		//$data['periode_awal']=$this->fungsi->dateindo3('-',$periode_awal);		//$data['periode_akhir']=$this->fungsi->dateindo3('-',$periode_akhir);		$datenow=date('Y');		$data['results'] 	= $this->mdl_anggaran->getItemByDate($periode_awal,$periode_akhir); 		$data['date_now']=$this->fungsi->dateindo3('-',date('Y-m-d'));        $html = $this->load->view('anggaran/anggaran_report_pdf', $data, true);        $this->pdf->pdf_create($html, 'anggaran','letter','landscape');	}
	
	function update($id)
	{
		if ($this->can_update() == FALSE){
			redirect('auth/failed');
		}
			
		$this->open();
		
		$data['result'] 		= $this->mdl_anggaran->getItemById($id);
		 
		$data['id_anggaran'] = $data['result']->row()->id_anggaran;		$data['tanggal'] = $data['result']->row()->tanggal;				$data['uraian'] = $data['result']->row()->uraian;				$data['namafile'] = $data['result']->row()->namafile;				$data['tahun'] = $data['result']->row()->tahun;		 
		$this->load->view('anggaran/anggaran_edit', $data);
 
		$this->close();
	}
	
	function process_update() 	{ 		if ($this->can_update() == FALSE){			redirect('auth/failed');		}		$this->open();				$data3['count_result'] = $this->input->post('count_result');		for ($i = 1; $i <= $data3['count_result']; $i++) {  		# Data		$data['id']					= $this->input->post('id'.$i);		$data['id_anggaran'] 			= $this->input->post('id_anggaran'.$i);		$data['tanggal'] 			= $this->input->post('tanggal'.$i);		 				$data['parawaba'] 			=  $this->input->post('parawaba'.$i);		$data['kategori'] 			=  $this->input->post('kategori'.$i);				 		$data['kredit'] 		= str_replace(" ", "", $this->input->post('kredit'.$i));		$data['kredit'] 		= str_replace(".", "", $data['kredit']);		$data['kredit'] 		= str_replace(",", ".", $data['kredit']);		 				 		$this->mdl_anggaran->update($data['id'], $data);				 		}			redirect('anggaran'); 		$this->close();			}
	
	function delete($id)
	{
		if ($this->can_delete() == FALSE){
			redirect('auth/failed');
		}
		
		$this->mdl_anggaran->delete($id);
		$this->session->set_flashdata('message', 'Data anggaran Berhasil dihapus.');
		redirect('anggaran');
	}	 	 
	
}